package org.xhnews.bo;

import java.util.Vector;

import org.xhnews.constant.Constant;
import org.xhnews.database.DatabaseManager;
import org.xhnews.download.DownloadManager;
import org.xhnews.parsers.CityPullXMLParser;
import org.xhnews.preferences.Preferences;
import org.xhnews.to.City;

public class WeatherBo {
	/*
	 * 获得最新天气概要信息
	 */
	public static Vector getWeathers() {
		return DatabaseManager.getWeathers(Preferences.location_city_code);
	}

	// getWeatherDetails() to add.
    /*
     * 获得全国所有省份
     */
	public static Vector getProvinces() {
		return DatabaseManager.getProvinces();
	}
    /*
     * 根据省份ID获得该省所有城市
     */
	public static Vector getCities(int provinceID) {
		return DatabaseManager.getCities(provinceID);
	}
    /*
     * 设置选择的城市
     */
	public static void setPreferCity(City city) {
		Preferences.location_city_id = city.getId();
		Preferences.location_city_name = city.getName();
		Preferences.location_city_code = city.getCode();
		Preferences.location_province_id = city.getProvinceID();
		Preferences.save();
	}
    /*
     * 更新天气概要信息
     */
	public static void updateWeathers() {
		Vector weathers = DownloadManager.donwloadWeathers(
				Preferences.server_address+Constant.URL_WEATHER, Preferences.location_city_code);
		DatabaseManager.updateWeathers(weathers, Preferences.location_city_code);
	}
    /*
     * 更新天气详细信息
     */
	public static void updateWeatherDetails() {
		DownloadManager.downloadAndSaveWeatherDetails(Preferences.server_address+Constant.URL_WEATHER,
				Constant.PATH_DATA, Preferences.location_city_code);
	}
	public void initCitiesAndProvincesDB(){		
		CityPullXMLParser.readCityToDB(getClass().getResourceAsStream(Constant.FILE_CITIES), "utf-8");
	}
}
